using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using Fieldbook.Common;
using Fieldbook.DAL;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace Fieldbook.Business
{
    public static class FieldController
    {
        #region Fields
        public static List<Field> LoadHouseHoldFields(long userID)
        {
            List<Field> res = null;
            try
            {
                res = FieldDAO.LoadHouseHoldFields(userID, "");
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static bool InsertFiled(Field field)
        {
            bool res = false;
            try
            {
                res = FieldDAO.InsertField(field);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static long? CheckHouseHoldField(long userID, long fieldID)
        {
            long? res = null;
            try
            {
                res = FieldDAO.CheckHouseHoldField(userID, fieldID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static List<Field> LoadMapFileds(long userID, int mapID)
        {
            List<Field> res = null;
            try
            {
                res = FieldDAO.LoadMapFields(userID, mapID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading fields for provided map", ex);
            }
            return res;
        }

        #endregion

        #region FildWorks
        public static List<FieldWork> LoadFiledWorks(long fieldID, int maximumRows, int startRowIndex)
        {
            List<FieldWork> res = null;
            try
            {
                res = FieldWorkDAO.LoadFieldWorks(fieldID, "", maximumRows, startRowIndex);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting fieldwork", ex);
            }
            return res;
        }

        public static int GetFieldWorkCount(long fieldID, int maximumRows, int startRowIndex)
        {
            int res = 0;
            try
            {
                res = FieldWorkDAO.GetFieldWorkCount(fieldID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static bool InsertFieldWork(FieldWork fw, Field.FieldState? fieldState,
                                            List<FieldWorkOperation> operations, 
                                            List<FieldWorkMachine> machines, 
                                            List<FieldWorkResource> resources)
        {
            bool res = true;
            DbTransaction transaction = null;
            try
            {
                SqlDatabase db = Utilities.GetDatabase();
                transaction = Utilities.BeginTransaction(db);
                FieldWorkDAO.InsertFieldWork(fw, transaction, db);
                foreach (FieldWorkOperation operation in operations)
                {
                    operation.FieldWorkID = (long)fw.FieldWorkID;
                    FieldWorkOperationDAO.InsertFieldWorkOperation(operation, transaction, db);
                }
                foreach (FieldWorkMachine machine in machines)
                {
                    machine.FieldWorkID = (long)fw.FieldWorkID;
                    FieldWorkMachineDAO.InsertFieldWorkMachine(machine, transaction, db);
                }
                foreach (FieldWorkResource resource in resources)
                {
                    resource.FieldWorkID = (long)fw.FieldWorkID;
                    FieldWorkResourceDAO.InsertFieldWorkResource(resource, transaction, db);
                    long? houseHoldID = HouseholdDAO.GetHouseHoldID(fw.LoggerID);
                    long? storageID = StorageItemDAO.GetStorageID(resource.ResourceID, (long)houseHoldID);
                    StorageItemDAO.UpateStorageQuota((long)storageID, resource.Quota * -1, transaction, db);
                }
                FieldDAO.UpdateFieldState(fw.FieldID, fieldState, transaction, db);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null) { transaction.Rollback(); }
                LogManager.LogException("Error inserting storage transaction", ex);
                res = false;
            }
            finally
            {
                Utilities.EndTransaction(transaction);
            }
            return res;
        }
        #endregion

        #region FiledMaps
        public static List<FieldMap> LoadFieldMaps()
        {
            List<FieldMap> res = null;
            try
            {
                res = FieldMapDAO.LoadFieldMaps();
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting field maps", ex);
            }
            return res;
        }

        public static FieldMap GetFieldMap(int fieldMapID)
        {
            FieldMap res = null;
            try
            {
                res = FieldMapDAO.GetFieldMap(fieldMapID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting field map", ex);
            }
            return res;
        }

        public static List<FieldMap> LoadHouseHoldFieldMaps(long userID)
        {
            List<FieldMap> res = null;
            try
            {
                res = FieldMapDAO.LoadHouseHoldFieldMaps(userID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error retreiving user field maps", ex);
            }
            return res;
        }

        public static List<FieldMap> LoadFreeFieldMaps(long userID)
        {
            List<FieldMap> res = null;
            try
            {
                res = FieldMapDAO.LoadFreeFieldMaps(userID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error retreiving free field maps", ex);
            }
            return res;
        }

        public static bool InsertFieldMap(FieldMap map)
        {
            bool res = false;
            try
            {
                res = FieldMapDAO.InsertFieldMap(map);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error inserting field map", ex);
            }
            return res;

        }
        #endregion
    }
}
